package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.sun.mail.imap.protocol.INTERNALDATE;
import dao.BlacklistDao;
import domin.*;
import lombok.extern.slf4j.Slf4j;
import service.ActorAssRoleService;
;
import service.BlacklistsService;
import service.OrdinaryUserService;
import service.UsersService;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

@Slf4j
@WebServlet("/login.ctl")
public class LoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();

        try {
            String user_json = JSONUtil.getJSON(request);
            Users userToLogin = JSON.parseObject(user_json, Users.class);
            Actor currentActor = UsersService.getInstance().login(userToLogin);
            Integer id = currentActor.getId();
            if (currentActor == null) {
                message.put("error","用户名或密码不匹配");
                log.trace("用户名密码不匹配");
                response.getWriter().println(message);
                return;
            }
            else{
                ActorAssRole actorAssRoleCollection = ActorAssRoleService.getInstance().find(currentActor.getId());
                try {
                    Actor ifbl =BlacklistsService.getInstance().find(id).getActor();
                    if (ifbl.getId() == id) {
                        message.put("error", "您已经被加入黑名单");
                        response.getWriter().println(JSON.toJSONString(message));
                        return;
                    }
                }
                catch (Exception e){
                    Role role = actorAssRoleCollection.getRole();
                    message.put("actor", currentActor);
                    message.put("role", role.getRole());
                    if(role.getId()==2){
                        String keyword = currentActor.getName();
                        Collection<OrdinaryUser> ordinaryUsers = OrdinaryUserService.getInstance().getAllWithCondition(keyword);
                        message.put("ordinaryUser",ordinaryUsers);
                    }
                    message.put("message","登陆成功");
                    log.trace("登录成功");
                    response.getWriter().println(JSON.toJSONString(message));
                }
            }
        } catch (SQLException e) {
            message.put("message", "数据库操作异常"+e.getMessage());
            log.trace("数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "登录失败"+e.getMessage());
            log.trace("登录失败");
            response.getWriter().println(message+e.getMessage());
        }
    }

}